//-----------------------------------------------------------------------
// <copyright file="AddhttpCustomHeader.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <summary>
//    Adds an Http Header to a web site
// </summary>
//-----------------------------------------------------------------------
namespace Microsoft.Sdc.Tasks.Web.WebSite
{
    using System;
    using Microsoft.Sdc.Tasks.Configuration.Web;
    using Microsoft.Build.Framework;

    /// <summary>
    /// Adds a new Http Custom Header to the specified web site
    /// </summary>
    /// <remarks>
    /// <code><![CDATA[<Web.WebSite.AddhttpCustomHeader 
    ///     Description="description"
    ///     HeaderName="headerName"
    ///     HeaderValue="headerValue"
    ///     MachineName="machineName"/>]]></code>
    /// <para>where:</para>
    /// <para><i>description</i></para>
    /// <para>Web site description to add the Http Custom Header to</para>
    /// <para><i>identifier</i></para>
    /// <para>optional parameter to identify website when 'description' is not used</para>
    /// <para><i>HeaderName (Required)</i></para>
    /// <para>Name for the new custom header</para>
    /// <para><i>HeaderValue (Required)</i></para>
    /// <para>Http Custom Header string</para>
    /// <para><i>machineName</i></para>
    /// <para>Machine to operate on(defaults to localhost)</para>
    /// </remarks>
    /// <example>
    /// <code><![CDATA[
    /// <Project>
    ///     <Target Name="Test" >
    ///         <Web.WebSite.AddhttpCustomHeader
    ///             Description="DcPortal"
    ///             HeaderName="pics-label"
    ///             HeaderValue='PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true for "http://www.bbc2.co.uk" r (nz 1 vz 1 lz 1 oz 1 cb 1)'/>
    ///     </Target>
    /// </Project>
    /// ]]></code>    
    /// </example>    
    public class AddhttpCustomHeader : TaskBase
    {
        private string description;
        private string headerName;
        private string headerValue;
        private string identifier;
        private string machineName = "localhost";

        /// <summary>
        /// Machine to operate on
        /// </summary>
        public string MachineName
        {
            get { return this.machineName; }
            set { this.machineName = value; }
        }

        /// <summary>
        /// File extension for Http Custom Header
        /// </summary>
        /// <value>extension</value>
        /// <remarks>required</remarks>
        [Required]
        public string HeaderValue
        {
            get { return (this.headerValue ?? String.Empty); }
            set { this.headerValue = value; }
        }

        /// <summary>
        /// Description of web site to which we are adding the filter
        /// </summary>
        /// <value>A valid web site description</value>
        /// <remarks>If this parameter is not specified you must specify the site <seealso cref="headerName">headerName</seealso> 
        /// for the site you wish to apply the binding to</remarks>
        public string Description
        {
            get { return (this.description ?? String.Empty); }
            set { this.description = value; }
        }

        /// <summary>
        /// Gets or sets the identifier.
        /// </summary>
        /// <value>The identifier.</value>
        public string Identifier
        {
            get { return (this.identifier ?? String.Empty); }
            set { this.identifier = value; }
        }

        /// <summary>
        /// web site idenfitier for website to which we are adding the filter
        /// </summary>
        /// <value>A valid web site headerName</value>
        /// <remarks>If this parameter is not specified you must specify the site <seealso cref="Description">Description</seealso> 
        /// </remarks>
        [Required]
        public string HeaderName
        {
            get { return (this.headerName ?? String.Empty); }
            set { this.headerName = value; }
        }

        /// <summary>
        /// Performs the action of this task.
        /// </summary>
        protected override void InternalExecute()
        {
            int id = Utilities.CheckWebSiteDescriptionOrIdentifier(this.Description, this.Identifier, "AddHttpCustomHeader", this.MachineName);
            if (WebSite.Exists(this.MachineName, id))
            {
                Microsoft.Sdc.Tasks.Configuration.Web.WebSite web = WebSite.Load(this.MachineName, id);
                Microsoft.Sdc.Tasks.Configuration.Web.HttpHeaders httpCustomHeaders = new Microsoft.Sdc.Tasks.Configuration.Web.HttpHeaders(web);
                httpCustomHeaders.Add(this.HeaderName, this.HeaderValue);
                web.Save();
            }
        }
    }
}